<template>
  <div class="app-container">
    <el-form class="formList"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="88px">
      <el-row>
        <el-form-item label="门店名称"
          prop="storeName">
          <el-input v-model="queryParams.storeName"
            placeholder="请输入门店名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="状态"
          prop="dataStatus">
          <el-select v-model="queryParams.dataStatus"
            placeholder="请选择状态"
            clearable
            size="small">
            <el-option v-for="dict in dataStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="门店类型"
          prop="storeType">
          <el-select v-model="queryParams.storeType"
            placeholder="请选择门店类型"
            clearable
            size="small">
            <el-option v-for="dict in storeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="服务类型"
          prop="serviceType">
          <el-select v-model="queryParams.serviceType"
            placeholder="请选择服务类型"
            clearable
            size="small">
            <el-option v-for="dict in serviceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 22px">
          <el-button type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"
            size="mini"
            @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-row>

    </el-form>

    <el-row :gutter="10"
      class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['platform:store:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['platform:store:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['platform:store:remove']">删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch"
        @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table v-loading="loading"
        :data="storeList"
        border
        @selection-change="handleSelectionChange">
        <el-table-column type="selection"
          width="55"
          align="center" />
        <el-table-column label="门店Banner图"
          align="center"
          prop="storeImg">
          <template slot-scope="scope">
            <el-image v-if="scope.row.storeBannerOne != null  "
              slot="reference"
              :src="scope.row.storeBannerOne"
              class="image-rows"
              :preview-src-list="[scope.row.storeBannerOne]"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="门店名称"
          align="center"
          prop="storeName"
          min-width="120"
          :show-overflow-tooltip="true" />
        <el-table-column label="门店类型"
          align="center"
          prop="storeTypeStr"
          :show-overflow-tooltip="true" />
        <el-table-column label="服务类型"
          align="center"
          prop="serviceTypeStr"
          :show-overflow-tooltip="true" />
        <el-table-column label="联系人"
          align="center"
          prop="storeContacts"
          :show-overflow-tooltip="true" />
        <el-table-column label="联系电话"
          align="center"
          prop="storePhone"
          :show-overflow-tooltip="true" />
        <el-table-column label="门店详细地址"
          align="center"
          prop="storeAddress"
          :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{scope.row.storeProvince }}{{scope.row.storeCity }}{{scope.row.storeArea }}{{scope.row.storeAddress }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态"
          align="center"
          prop="dataStatus">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="更新时间"
          align="center"
          prop="createTime"
          width="180"
          sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作"
          fixed="right"
          width="150"
          align="center"
          class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="0>1"
              size="mini"
              type="text"
              icon="el-icon-zoom-in"
              @click="handleShowDetail(scope.row)"
              v-hasPermi="['platform:store:query']">查看</el-button>
            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['platform:store:edit']">修改</el-button>

            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handle(scope.row)"
              v-hasPermi="['platform:store:edit']">账号管理</el-button>

            <el-button v-if="scope.dataStatus == 0"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['platform:store:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <!-- 添加或修改门店信息对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="570px"
      append-to-body
      @keyup.enter.native="submitForm">
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">

        <el-form-item label="门店名称"
          prop="storeName">
          <el-input v-model="form.storeName"
            placeholder="请输入门店名称" />
        </el-form-item>
        <el-form-item label="联系人"
          prop="storeContacts">
          <el-input v-model="form.storeContacts"
            placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话"
          prop="storePhone">
          <el-input v-model="form.storePhone"
            placeholder="请输入联系电话" />
        </el-form-item>

        <el-form-item label="商户名称"
          prop="merchantName">
          <el-input v-model="form.merchantName"
            placeholder="请输入商户名称" />
        </el-form-item>

        <el-form-item label="商户id"
          prop="merchantId">
          <el-input v-model="form.merchantId"
            placeholder="请输入商户id" />
        </el-form-item>

        <el-form-item label="门店类型">
          <el-select v-model="form.storeType"
            placeholder="请选择"
            size="small">
            <el-option v-for="dict in storeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="服务类型">
          <el-select v-model="form.serviceType"
            placeholder="请选择"
            size="small">
            <el-option v-for="dict in serviceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营业时间"
          prop="occupationTime">
          <el-input v-model="form.occupationTime"
            placeholder="请输入营业时间" />
        </el-form-item>

        <!-- <el-form-item label="appid"
          prop="appid">
          <el-input v-model="form.appid"
            placeholder="请输入appid" />
        </el-form-item> -->

        <el-form-item label="门店地址"
          prop="storeProvince">
          <el-cascader ref="cascaderAddr"
            v-model="form.region"
            :options="options"
            clearable
            filterable
            class="input-rule"
            style="width: 430px"></el-cascader>
        </el-form-item>

        <el-form-item label="门店详细地址"
          prop="storeAddress">
          <el-input v-model="form.storeAddress"
            maxlength="80"
            show-word-limit
            placeholder="请输入门店详细地址" />
        </el-form-item>
        <el-form-item label="门店banner"
          prop="storeBanner">
          <imageArraySize :imageStr.sync="form.storeBanner"
            :key="form.storeBanner"
            :msg="'图片要求：宽高等于800px，大小1M内。'"
            :limit="10"
            ref="childsss"></imageArraySize>
        </el-form-item>
        <!-- <el-form-item label="门店详情图"
          prop="storeImg">
          <imageArraySize :imageStr.sync="form.storeImg"
            :key="form.storeImg"
            :msg="'图片要求：宽高等于800px，大小1M内。'"
            :limit="10"
            ref="childss"></imageArraySize>
        </el-form-item>

        <el-form-item label="详情视频"
          prop="storeVideo"
          style="height: 130px;">
          <div class="place-font">视频要求：大小8M内。</div>
          <videoUpload :videoStr.sync="form.storeVideo"></videoUpload>
        </el-form-item> -->

      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看门店信息对话框 -->
    <el-dialog :title="viewTitle"
      :visible.sync="viewOpen"
      width="700px"
      append-to-body>
      <el-form ref="detailForm"
        label-width="100px">
        <el-row>
          <el-col :span="12"
            v-for="(item, index) in showData"
            :key="index"
            style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          size="mini"
          @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStore,
  getStore,
  delStore,
  addStore,
  updateStore, updateStoreStatus
} from "@/api/platform/store";
import {
  getRegion
} from '@/api/common/regin'
import imageArraySize from "@/components/ImageUpload/imageArraySize";
import videoUpload from "@/components/ImageUpload/videoUpload";

export default {
  name: "Store",
  components: {
    imageArraySize, videoUpload
  },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店信息表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      //查看详细标题
      viewTitle: "",
      // 是否显示弹出层
      open: false,
      //是否显示查看详细弹出层
      viewOpen: false,
      //查看详细数据
      showData: [],
      // 状态字典
      dataStatusOptions: [],
      storeOptions: [],
      serviceOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        platformUid: null,
        apppid: null,
        serviceType: null,
        storeType: null,
        storeName: null,
        storeContacts: null,
        storePhone: null,
        storeProvince: null,
        storeCity: null,
        storeArea: null,
        storeAddress: null,
        storeBanner: null,
        storeImg: null,
        storeLongitude: null,
        storeLatitude: null,
        dataStatus: null,
        auditStatus: null,
        createUid: null,
        createName: null,
        createTime: null,
        updateUid: null,
        updateName: null,
        updateTime: null,
        auditUid: null,
        auditName: null,
        auditTime: null,
        customerUid: null
      },
      columns: [{
        label: '客户id',
        dataIndex: 'storeUid',
      },
      {
        label: '平台uid',
        dataIndex: 'platformUid',
      },
      {
        label: 'appid',
        dataIndex: 'apppid',
      },
      {
        label: '门店名称',
        dataIndex: 'storeName',
      },

      {
        label: '门店类型',
        dataIndex: 'storeTypeStr',
      },
      {
        label: '服务类型',
        dataIndex: 'serviceTypeStr',
      },

      {
        label: '联系人',
        dataIndex: 'storeContacts',
      },
      {
        label: '联系电话',
        dataIndex: 'storePhone',
      },
      {
        label: '门店省份',
        dataIndex: 'storeProvince',
      },
      {
        label: '门店城市',
        dataIndex: 'storeCity',
      },
      {
        label: '门店区域',
        dataIndex: 'storeArea',
      },
      {
        label: '门店详细地址',
        dataIndex: 'storeAddress',
      },
      {
        label: '门店banner',
        dataIndex: 'storeBanner',
      },
      {
        label: '门店详情图',
        dataIndex: 'storeImg',
      },
      {
        label: '经度',
        dataIndex: 'storeLongitude',
      },
      {
        label: '纬度',
        dataIndex: 'storeLatitude',
      },
      {
        label: '状态（1有效/0无效)',
        dataIndex: 'dataStatus',
      },
      {
        label: '审核状态',
        dataIndex: 'auditStatus',
      },
      {
        label: '审核状态',
        dataIndex: 'createUid',
      },
      {
        label: '审核状态',
        dataIndex: 'createName',
      },
      {
        label: '审核状态',
        dataIndex: 'createTime',
      },
      {
        label: '审核状态',
        dataIndex: 'updateUid',
      },
      {
        label: '审核状态',
        dataIndex: 'updateName',
      },
      {
        label: '审核状态',
        dataIndex: 'updateTime',
      },
      {
        label: '审核人ID',
        dataIndex: 'auditUid',
      },
      {
        label: '审核人名称',
        dataIndex: 'auditName',
      },
      {
        label: '审核人时间',
        dataIndex: 'auditTime',
      },
      {
        label: '客户id',
        dataIndex: 'customerUid',
      },
      ],
      // 表单参数
      form: {},
      options: [],
      // 表单校验
      rules: {

        storeName: [{
          required: true,
          message: "门店名称不能为空",
          trigger: "blur"
        }],
        storeContacts: [{
          required: true,
          message: "联系人不能为空",
          trigger: "blur"
        }],
        storePhone: [{
          required: true,
          pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          message: "请输入正确的手机号码",
          trigger: ["blur", "change"]
        }],

      }
    };
  },
  created () {
    this.getList();
    this.getDicts("sys_data_status").then(response => {
      this.dataStatusOptions = response.data;
    });

    this.getDicts("proBoxService_itemType").then(response => {
      this.serviceOptions = response.data;
    });




    this.getDicts("STORE_TYPE").then(response => {
      this.storeOptions = response.data;
    });

    // 产地
    getRegion().then((response) => {
      this.options = response.data;
    });
  },
  methods: {
    imgs (value) {
      let arr = value.split(",");
      return arr;
    },
    // 状态修改
    handleStatusChange (row) {
      let text = row.dataStatus === "0" ? "无效" : "有效";
      this.$confirm('确定要把门店名称为"' + row.storeName + '"设为' + text + '吗？', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return updateStoreStatus(row.storeUid, row.dataStatus);
      }).then(() => {
        this.msgSuccess("操作成功");
        this.getList();
      }).catch(function () {
        row.dataStatus = row.dataStatus === "0" ? "1" : "0";
      });
    },
    /** 查询门店信息列表 */
    getList () {
      this.loading = true;
      listStore(this.queryParams).then(response => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    dataStatusFormat (row, column) {
      return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        storeUid: null,
        platformUid: null,
        apppid: null,
        storeName: null,
        storeContacts: null,
        storePhone: null,
        storeProvince: null,
        storeCity: null,
        storeArea: null,
        storeAddress: null,
        storeBanner: null,
        storeImg: null,
        storeLongitude: null,
        storeLatitude: null,
        dataStatus: null,
        auditStatus: "0",
        createUid: null,
        createName: null,
        createTime: null,
        updateUid: null,
        updateName: null,
        updateTime: null,
        auditUid: null,
        auditName: null,
        auditTime: null,
        customerUid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.storeUid)
      this.names = selection.map(item => item.storeName);
      this.single = selection.length !== 1
      this.multiple = !selection.length
      //用于禁用删除
      if (this.multiple == false) {
        for (var i = 0; i < selection.length; i++) {
          var element = selection[i];
          if ("1" === element.dataStatus) {
            this.multiple = true;
            break;
          }
        }
      }
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加门店信息";
    },
    /** 查看详情按钮操作 **/
    handleShowDetail (row) {
      this.viewOpen = true;
      this.viewTitle = "查看门店信息";
      this.showData = [];
      this.columns.forEach(item => {
        this.showData.push({
          label: item.label,
          value: row[item.dataIndex]
        })
      })
    },
    handCloseDetail () {
      this.viewOpen = false
      this.showData = []
    },

    handle (row) {
      this.$router.push({
        path: "/store/employee/service/user", query: {
          storeUid: row.storeUid,
        }
      });

    },

    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const storeUid = row.storeUid || this.ids
      getStore(storeUid).then(response => {
        this.form = response.data;
        this.form.region = response.region;
        this.open = true;
        this.title = "修改门店信息";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs.childsss.selectImg();
      this.$refs["form"].validate(valid => {
        if (this.options) {
          if (this.$refs['cascaderAddr'] != null && this.$refs['cascaderAddr'].getCheckedNodes()[0] !=
            null) {
            let areaName = this.$refs['cascaderAddr'].getCheckedNodes()[0].pathLabels
            if (this.form.region) {
              this.form.storeProvince = areaName[0];
              this.form.storeCity = areaName[1];
              this.form.storeArea = areaName[2];
            }
          }
        }
        if (valid) {
          if (this.form.storeUid != null) {
            updateStore(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStore(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const storeUids = row.storeUid || this.ids;
      this.$confirm('是否确认删除门店名称为"' + this.names + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delStore(storeUids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('platform/store/export', {
        ...this.queryParams
      }, `platform_store.xlsx`)
    }
  }
};
</script>
